<?php

class Frontend extends MY_Controller {

	public function __construct() {
		parent::__construct();

		$this->initialize();
	}

	private function initialize() {
		$lang = $this->input->cookie('lang');
		$lang = empty($lang) ? 'zh' : $lang;
		
		switch ($lang) {
			case 'en':
				$this->lang->load('ui', 'english');
				break;
			case 'fr':
				$this->lang->load('ui', 'french');
				break;
			case 'zh':
			default:
				$this->lang->load('ui', 'chinese');
				break;
		}

		$this->set_title($lang == 'en' ? 'English Title' : '中文标题');
		$this->set_layout('frontend/layout');
		$this->set_js('LANG', "'{$lang}'");
		$this->set_js('SITE_URL', '\'' . SITE_URL . '\'');
		$this->load_js(array('jquery.min', 'jquery.tmpl.min', 'lib', 'frontend'));
		$this->load_css(array('global', 'lib', 'frontend'));
		
		$this->load->model('news_category_model');
		$newsCategories = $this->news_category_model->records(array(array('id'),array('name_'.$lang, 'name')));
		
		$this->set_data(array(
			'g_lang' => $lang,
			'newsCategories' => $newsCategories
		));
	}

	public function language() {
		//通过get参数来设置语言
		$lang = $this->uri->segment('3');
		if ($lang) {
			$this->input->set_cookie(array(
				'name' => 'lang',
				'value' => $lang,
				'expire' => '86500'
			));
		} else {
			//没有参数 则取cookie中的值
			$lang = $this->input->cookie('lang');
			if (!$lang) {
				$lang = 'zh';
			}
		}

		$from = $this->input->server('HTTP_REFERER');
		header("Location: {$from}");
		exie(0);
	}

	protected function get_language() {
		$lang = $this->input->cookie('lang');

		switch ($lang) {
			case 'en':
			case 'fr':
				break;
			default:
				$lang = 'zh';
				break;
		}

		return $lang;
	}

}